/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.vojna.ant_alg.algorithms;

import pl.vojna.ant_alg.Problem;
import pl.vojna.ant_alg.Utils;
import pl.vojna.ant_alg.lokal_search_alg.TabuSearch;

/**
 *
 * @author rusak
 */
public class MMAntSystemLocalSearch extends MMAntSystem {

    public MMAntSystemLocalSearch(double pheromoneVanish, double pheromoneStrength, 
            double minPheromone, double maxPheromone) {
        super(pheromoneVanish, pheromoneStrength, minPheromone, maxPheromone);
    }
    

	@Override
	public int[] solve(Problem problem, int popSize, Boolean isDebug,
			int ASIterations) {
		int tabuIterations = 100;
		int best[] = null;
		int solutions[][] = new int[popSize + 1][problem.getSize()];
		int optSolutions[][] = new int[popSize + 1][problem.getSize()];
		final double pheromoneTrails[][] = initPhereomone(problem.getSize());
		TabuSearch tabu = new TabuSearch();
		for (int k = 0; k < ASIterations; k++) {
			Ant population[] = results(problem, popSize, isDebug,
					pheromoneTrails);

			solutions[0] = Utils.newIdPermutation(problem.getSize());
			for (int i = 1; i < popSize + 1; i++) {
				solutions[i] = population[i - 1].getSolution();
			}

			for (int i = 0; i < popSize + 1; i++) {
				optSolutions[i] = tabu.improve(solutions[i], problem,
						tabuIterations);
			}
			if (k == 0) {
				best = optSolutions[0];
			}
			double bestCost = Utils.solutionCost(problem, best);
			int pretender[];
			double pretenderCost;
			for (int i = 1; i < popSize + 1; i++) {
				pretender = optSolutions[i];
				pretenderCost = Utils.solutionCost(problem, pretender);
				if (bestCost > pretenderCost) {
					best = pretender;
					bestCost = pretenderCost;
					//System.out.println("BETTER: ");
				//	Utils.showSolution(best);
				//	System.out.println("COST: " + bestCost);
				}
			}
		}
		return best;
	}

}
